(module graph mzscheme
  (provide srl-graph%)
  
  (require (lib "mred.ss" "mred")
           (lib "class.ss")
           "gui.ss")
  
  (define pen (instantiate pen% ("black" 2 'solid)))
  (define brush (instantiate brush% ("black" 'solid)))
  
  (define srl-graph%
    (class vertical-pane%
      (super-new)
      
      (init label)
      (init background)
      (init-field x-scale)
      (init-field y-scale)
      
      (define (initial-points) (list (cons 20 115)))
      (define-handler initial-points)
      
      (define points (initial-points))
      
      (define/public (redraw)
        (send this-graph clear)
        (send this-graph initial-draw)
;        (display (initial-points))
;        (newline)
        (map (lambda (x) (add-point (car x) (cdr x)))
             (initial-points)))
      
      (define (add-raw-point! x y)
        (set! points (cons (cons x y) points))
        (add-raw-point x y))
      
      (define (add-raw-point x y)
        (send this-graph draw-point x y)
        (send this-graph draw-line x y (car (cadr points)) (cdr (cadr points))))
      
      (define/public (add-point week miles)
        (add-raw-point! (+ 20 (* x-scale week)) (- 115 (* y-scale miles))))
      
      (define this-label (new message% (parent this) (label label)))
      (define this-graph (new graph%
                              (parent this)
                              (background background)
                              (min-width (send background get-width))
                              (min-height (send background get-height))))
      
      (sleep/yield 1)
      (send this-graph initial-draw)))
  
  (define graph%
    (class canvas%
      (super-new)
      
      (init-field (background #f))
      
      (define/override (on-event event) (send (send this get-parent) redraw))
      
      (define dc (send this get-dc))
      (unless (send dc ok?) (error "The drawing context is NOT okay. Graphs cannot be drawn."))
      (send dc set-pen pen)
      (send dc set-brush brush)
      
      (define/public (clear) (send dc clear))
      
      (define/public (initial-draw)
        (when (and background (send background ok?))
          (send dc draw-bitmap background 0 0)))
      
      (define/public (draw-point x y)
        (send dc draw-point x y))
      
      (define/public (draw-line x1 y1 x2 y2)
        (send dc draw-line x1 y1 x2 y2)))))
  
#|  (define frame (new frame% (label "Graph")))
  (define background (make-object bitmap% "~/srlsrc/images/graph-weekly.png" 'png))
  (send frame show #t)
  (define graph (new srl-graph% (parent frame)
                     (label "Weekly Mileage")
                     (background background)
                     (min-width (send background get-width))
                     (min-height (send background get-height))
                     (x-scale (/ 270 52))
                     (y-scale 1)))
  (send graph add-point 1 30)
  (send graph add-point 2 33)
  (send graph add-point 3 36)
  (send graph add-point 4 32)
  (send graph add-point 5 38)
  (send graph add-point 52 65))|#